//
//  PlayInView.m
//  playin
//
//  Created by playin on 2019/2/26.
//  Copyright © 2019年 playin. All rights reserved.
//

#import "PlayInView.h"
#import "PITouchView.h"
#import "PIVideoDisplay.h"
#import "PIDeviceInfo.h"
#import "PICommon.h"

@interface PlayInView () <PITouchViewDelegate>

@property (nonatomic, strong) PIVideoDisplay *display;
@property (nonatomic, strong) PITouchView *touchView;

@property (nonatomic, assign) int orientation;
@property (nonatomic, assign) CGFloat originalW;
@property (nonatomic, assign) CGFloat originalH;
@property (nonatomic, assign) CGFloat deviceW;
@property (nonatomic, assign) CGFloat deviceH;
@property (nonatomic, assign) CGRect displayRect;
@property (nonatomic, strong) NSDictionary *playDic;

@end

@implementation PlayInView

- (instancetype)initWithFrame:(CGRect)frame
                  deviceWidth:(CGFloat)deviceWidth
                 deviceHeight:(CGFloat)deviceHeight
                  orientation:(int)orientation
                       osType:(int)osType {
    
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = UIColor.blackColor;
        
        self.originalW = frame.size.width;
        self.originalH = frame.size.height;
        
        self.deviceW = deviceWidth;
        self.deviceH = deviceHeight;
        
        CGFloat scale = deviceWidth / deviceHeight;
        CGFloat displayX = 0;
        CGFloat displayW = 0;
        CGFloat displayH = 0;
        CGFloat displayY = 0;
       
        displayW = self.originalW;
        displayH = displayW / scale;
        if (displayH > self.originalH) {
            displayH = self.originalH;
            displayW = displayH * scale;
        }
        displayX = self.originalW / 2.0 - displayW / 2.0;
        displayY = self.originalH / 2.0 - displayH / 2.0;
        self.displayRect = CGRectMake(displayX, displayY, displayW, displayH);

        self.display = [[PIVideoDisplay alloc] initWithFrame:self.displayRect];
        [self addSubview:self.display];
        
        self.touchView = [[PITouchView alloc] initWithFrame:self.displayRect];
        self.touchView.delegate = self;
        [self addSubview:self.touchView];

    }
    return self;
}

- (void)displayPreviousPixelBuffer:(CVPixelBufferRef)pixelBuffer {
    
    CVPixelBufferRetain(pixelBuffer);
    [self.display displayPreviousPixelImageBuffer:pixelBuffer];
    CVPixelBufferRelease(pixelBuffer);
}

- (void)displayPixelBuffer:(CVPixelBufferRef)pixelBuffer {
    
    CVPixelBufferRetain(pixelBuffer);
    [self.display displayPixelBuffer:pixelBuffer];
    CVPixelBufferRelease(pixelBuffer);
}

- (void)onPITouchViewTouched:(NSData *)data {
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(onPlayInViewTouch:)]) {
        [self.delegate onPlayInViewTouch:data];
    }
}

#pragma mark -

- (void)destroy {
    
    [self.touchView removeFromSuperview];
    self.touchView = nil;
    [self.display removeFromSuperview];
    self.display = nil;
}

- (void)dealloc {
    
    NSLog(@"***** [%@ dealloc] *****", [self class]);
}

@end
